package com.loctek.importexport.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;


/**
 * Created by slf on 15/9/28.
 */
public class MenuColorizer {

    private MenuColorizer() {
    }

    /**
     * Sets a color filter on all menu icons, including the overflow button (if it exists)
     */
    public static void colorMenu(final Activity activity, final Menu menu, final int color) {
        colorMenu(activity, menu, color, 0);
    }

    /**
     * Sets a color filter on all menu icons, including the overflow button (if it exists)
     */
    public static void colorMenu(final Activity activity, final Menu menu, final int color, final int alpha) {
        for (int i = 0, size = menu.size(); i < size; i++) {
            final MenuItem menuItem = menu.getItem(i);
            colorMenuItem(menuItem, color, alpha, activity);
            if (menuItem.hasSubMenu()) {
                final SubMenu subMenu = menuItem.getSubMenu();
                for (int j = 0; j < subMenu.size(); j++) {
                    colorMenuItem(subMenu.getItem(j), color, alpha, activity);
                }
            }
        }
        final View home = ViewHelpers.getActionBarHomeView(activity); // modified by mstevens
        if (home != null) {
            home.post(new Runnable() {
                @Override
                public void run() {
                    colorOverflow(activity, color, alpha);
                }
            });
        }
    }

    /**
     * Sets a color filter on a {@link MenuItem}
     */
    public static void colorMenuItem(final MenuItem menuItem, final int color, Context context) {
        colorMenuItem(menuItem, color, 0, context);
    }

    /**
     * Sets a color filter on a {@link MenuItem}
     */
    public static void colorMenuItem(final MenuItem menuItem, final int color, final int alpha, Context context) {
        Drawable drawable = menuItem.getIcon();
        if (drawable != null) {

            drawable = DrawableCompat.wrap(drawable);
            DrawableCompat.setTint(drawable, context.getResources().getColor(color));
            menuItem.setIcon(drawable);
            if (alpha > 0) {
                drawable.setAlpha(alpha);
            }
//            drawable.setAlpha(alpha);
//            // If we don't mutate the drawable, then all drawable's with this id will have a color filter applied to it.
//            drawable.mutate();
//            drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
//            if (alpha > 0) {
//                drawable.setAlpha(alpha);
//            }
        }
    }

    /**
     * Sets a color filter on the OverflowMenuButton in an ActionBar or Toolbar
     */
    public static void colorOverflow(final Activity activity, final int color) {
        colorOverflow(activity, color, 0);
    }

    /**
     * Sets a color filter on the OverflowMenuButton in an ActionBar or Toolbar
     */
    @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    public static void colorOverflow(final Activity activity, final int color, final int alpha) {
        final ImageButton overflow = getOverflowMenu(activity);
        if (overflow != null) {
            overflow.setColorFilter(color);
            if (alpha > 0) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    overflow.setImageAlpha(alpha);
                } else {
                    overflow.setAlpha(alpha);
                }
            }
        }
    }

    /* Find that OverflowMenuButton */
    private static ImageButton getOverflowMenu(final Activity activity, final ViewGroup... viewGroup) {
        final ViewGroup group;
        if (viewGroup == null || viewGroup.length == 0) {
            final int resId = activity.getResources().getIdentifier("action_bar", "id", "android");
            if (resId != 0) {
                group = (ViewGroup) activity.findViewById(resId);
            } else {
                group = (ViewGroup) activity.findViewById(android.R.id.content).getRootView();
            }
        } else {
            group = viewGroup[0];
        }
        ImageButton overflow = null;
        for (int i = 0, l = group.getChildCount(); i < l; i++) {
            final View v = group.getChildAt(i);
            if (v instanceof ImageButton && v.getClass().getSimpleName().equals("OverflowMenuButton")) {
                overflow = (ImageButton) v;
            } else if (v instanceof ViewGroup) {
                overflow = getOverflowMenu(activity, (ViewGroup) v);
            }
            if (overflow != null) {
                break;
            }
        }
        return overflow;
    }
}
